package com.jcxy.service.impl;

import com.jcxy.mapper.ProcessMapper;
import com.jcxy.pojo.Burse;
import com.jcxy.pojo.BurseDetail;
import com.jcxy.pojo.Process;
import com.jcxy.pojo.ProcessUser;
import com.jcxy.service.ProcessService;
import com.jcxy.util.DateUtil;
import com.jcxy.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ProcessServiceImpl implements ProcessService {

    @Autowired
    private ProcessMapper processMapper;

    @Override
    public PageBean queryListByPage(Map dto, Integer page, Integer limit) {

        Integer count = processMapper.queryProcessCount(dto);
        PageBean pageBean = new PageBean(page, limit);
        dto.put("pageIndex",pageBean.getPageIndex());
        dto.put("limit",pageBean.getLimit());
        List<Process> statusList = processMapper.queryProcessByPage(dto);

        pageBean.setResponseData(count,statusList);
        return pageBean;
    }

    /**
     * 添加流程
     * @param dto
     * @return
     * @throws Exception
     */
    @Transactional
    public Process addProcess(Map dto) throws Exception {

        //装配流程实体类
        Process process = new Process();
        process.setProcess_title(dto.get("process_title").toString());
        process.setApply_time(DateUtil.getCurrDateStr("yyyy-MM-dd HH:mm:ss",new Date()));
        process.setProcess_type(dto.get("process_type").toString());
        process.setProcess_status_id(dto.get("process_status_id").toString());
        process.setAudit_status_id("8");
        process.setUser_id(dto.get("user_id").toString());
        process.setProgress_level("1");

        processMapper.insertProcess(process);

        return process;
    }

    /**
     * 添加流程审核记录
     * @param user_id 审核人id
     * @param process_id 流程id
     * @throws Exception
     */
    @Transactional
    public void insertProcessUser(String user_id,String process_id) throws Exception {

        //装配流程审核实体类
        ProcessUser processUser = new ProcessUser();
        processUser.setProcess_id(process_id);
        processUser.setUser_id(user_id);
        processUser.setIsAudit("0");

        processMapper.insertProcessUser(processUser);

    }

    @Override
    @Transactional
    public void addBurseProcess(Map dto) throws Exception {

        //添加流程
        Process process = addProcess(dto);

        //装配Burse实体类
        Burse burse = new Burse();
        burse.setCustomer(dto.get("customer").toString());
        burse.setPayment(dto.get("payment").toString());
        burse.setBurse_desc(dto.get("burse_desc").toString());
        burse.setProve_user_id(dto.get("prove_user_id").toString());
        burse.setProcess_id(process.getProcess_id());

        //插入费用报销信息
        processMapper.insertBurse(burse);

        Object o = dto.get("burseDetail");
        if (o != null){
            String[] burseDetail = new String[1];
            if (o instanceof String[]){
                burseDetail = (String[]) o;
            }else {
                burseDetail[0] = (String) o;
            }
            int len = burseDetail.length;
            for (int i = 0; i < len; i++){
                //装配报销费用明细
                BurseDetail detail = new BurseDetail();
                detail.setBurse_time(dto.get("burse_time_"+burseDetail[i]).toString());
                detail.setSubject_id(dto.get("subject_id_"+burseDetail[i]).toString());
                detail.setBurse_reason(dto.get("burse_reason_"+burseDetail[i]).toString());
                detail.setBurse_invoices(Integer.valueOf(dto.get("burse_invoices_"+burseDetail[i]).toString()));
                detail.setBurse_money(Float.valueOf(dto.get("burse_money_"+burseDetail[i]).toString()));
                detail.setBurse_id(burse.getBurse_id());

                //插入报销费用明细
                processMapper.insertBurseDetail(detail);
            }
        }

        //添加流程审核记录
        insertProcessUser(dto.get("auditUser_id").toString(),process.getProcess_id());
    }

    @Override
    @Transactional
    public void addEvectionProcess(Map dto) throws Exception {

        //添加流程
        Process process = addProcess(dto);

        dto.put("process_id",process.getProcess_id());
        processMapper.insertEvection(dto);

        //添加流程审核记录
        insertProcessUser(dto.get("auditUser_id").toString(),process.getProcess_id());
    }

    @Override
    @Transactional
    public void addOvertimeProcess(Map dto) throws Exception {

        //添加流程
        Process process = addProcess(dto);

        dto.put("process_id",process.getProcess_id());
        processMapper.insertOvertime(dto);

        //添加流程审核记录
        insertProcessUser(dto.get("auditUser_id").toString(),process.getProcess_id());
    }

    @Override
    @Transactional
    public void addRegularProcess(Map dto) throws Exception {

        //添加流程
        Process process = addProcess(dto);

        dto.put("process_id",process.getProcess_id());
        processMapper.insertRegular(dto);

        //添加流程审核记录
        insertProcessUser(dto.get("auditUser_id").toString(),process.getProcess_id());
    }

    @Override
    @Transactional
    public void addHolidayProcess(Map dto) throws Exception {

        //添加流程
        Process process = addProcess(dto);

        dto.put("process_id",process.getProcess_id());
        processMapper.insertHoliday(dto);

        //添加流程审核记录
        insertProcessUser(dto.get("auditUser_id").toString(),process.getProcess_id());
    }

    @Override
    @Transactional
    public void addResignProcess(Map dto) throws Exception {

        //添加流程
        Process process = addProcess(dto);

        dto.put("process_id",process.getProcess_id());
        processMapper.insertResign(dto);

        //添加流程审核记录
        insertProcessUser(dto.get("auditUser_id").toString(),process.getProcess_id());
    }

    @Override
    public PageBean queryAuditProcessListByPage(Map dto, Integer page, Integer limit) {

        Integer count = processMapper.queryAuditProcessCount(dto);
        PageBean pageBean = new PageBean(page, limit);
        dto.put("pageIndex",pageBean.getPageIndex());
        dto.put("limit",pageBean.getLimit());
        List<Process> statusList = processMapper.queryAuditProcessByPage(dto);

        pageBean.setResponseData(count,statusList);
        return pageBean;
    }

    @Override
    @Transactional
    public void auditProcess(Map dto) throws Exception {

        //获取当前审核人的类型：0-部门，1-人事，2-财务
        String cur_auditType = dto.get("cur_auditType").toString();
        //下一步审核人的类型：0-流程已结束，1-人事，2-财务
        String next_auditType = dto.get("next_auditType").toString();
        //获取流程类型：burse、evection、overtime、regular、holiday、resign
        String process_type = dto.get("process_type").toString();
        //获取当前审核人设置的审核状态：10-已批准，11-未通过
        String audit_status_id = dto.get("audit_status_id").toString();

        //判断是否有下一步审核
        boolean isNext = true;
        if("11".equals(audit_status_id) || "0".equals(next_auditType)){
            //如果当前审核人不通过，或者下一步审核人类型为0，则没有下一步审核
            isNext = false;
            //设置流程结束时间
            dto.put("end_time",new Date());
        }else {
            //还有下一步审核，将审核状态设置为处理中
            dto.put("audit_status_id","9");
            dto.put("end_time",null);
        }
        /***********************修改流程基本信息START*************************/
        //修改流程基本信息
        processMapper.updateProcess(dto);
        //修改当前用户的审核流程信息
        processMapper.updateUserAuditProcess(dto);
        /***********************修改流程基本信息END*************************/


        /***********************插入审核人意见START*************************/
        //判断流程类型
        if("burse".equals(process_type)){

            processMapper.updateBurseAdvice(dto);

        }else if("evection".equals(process_type)){

            processMapper.updateEvectionAdvice(dto);

        }else if("overtime".equals(process_type)){

            processMapper.updateOvertimeAdvice(dto);

        }else if("regular".equals(process_type)){

            processMapper.updateRegularAdvice(dto);

        }else if("holiday".equals(process_type)){

            processMapper.updateHolidayAdvice(dto);

        }else if("resign".equals(process_type)){

            processMapper.updateResignAdvice(dto);

        }
        /***********************插入审核人意见END*************************/


        /***********************插入下一步审核流程记录START*************************/
        //判断是否有下一步审核流程
        if (isNext){
            //有下一步，装配ProcessUser实体类
            ProcessUser processUser = new ProcessUser();
            processUser.setIsAudit("0");
            processUser.setUser_id(dto.get("audit_user_id").toString());
            processUser.setProcess_id(dto.get("process_id").toString());

            processMapper.insertProcessUser(processUser);
        }
        /***********************插入下一步审核流程记录END*************************/

    }
}
